<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/10
 * Time: 16:09
 */

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Models\Administrator;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;

class BaseController extends Controller
{
    public $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    
    /**
     * 核对管理员权限
     * @return bool
     */
    protected function checkPermission()
    {
        $token = Request()->header('token');
        $info = Administrator::with(['roles'])->where('remember_token',$token)->first();
        if($info){
            $roles = $info->toArray()['roles'];
            $permissions = [];
            if($roles) {
                $roleIds = array_column($roles, 'id');
                $permissions = $this->getPermission($roleIds);
            }
            $queryUrl = trim(Request()->getRequestUri(),'/');
            if(in_array('all',$permissions) || in_array($queryUrl,$permissions)){
                return true;
            }
        }
        return false;
    }
    /**
     * 获取权限标识
     * @param array $roleIdArr
     * @return array
     */
    protected function getPermission($roleIdArr = [])
    {
        if(!$roleIdArr){
            return [];
        }
        $permissionSlug = $permissionIdArr = [];
        if (array_search(1, $roleIdArr) !== false) {
            $permissionSlug = ['all'];
        }else{
            $permissionIds = Role::getPermissionId($roleIdArr);
            if($permissionIds){
                $permissionIds = $permissionIds->toArray();
                foreach($permissionIds as $k => $v){
                    $permissionIdArr = array_unique(array_merge($permissionIdArr,$v));
                }
            }
            $slugArr = Permission::whereIn('id',$permissionIdArr)->pluck('slug');
            if($slugArr){
                $permissionSlug = array_merge($permissionSlug,$slugArr->toArray());
            }
        }

        return $permissionSlug;
    }

    /**
     * 上传图片
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadImg()
    {
        $file = $this->request->file('file');
        if(!$file){
            return Response()->json(['code'=>201,'msg'=>'请上传图片']);
        }
        $folder_name = "/uploads/images/".date('Ym/d/',time());
        $upload_path = public_path() . "/" . $folder_name;
        $extension = strtolower($file->getClientOriginalExtension()) ?: 'png';
        $filename = time().rand(100,999).'.'. $extension;
        //将图片移动到我们目标存储路径中
        $file->move($upload_path , $filename);
        return Response()->json(['code'=>200,'msg'=>'上传成功','data'=>$folder_name.$filename]);

    }

    /**
     * 上传文件
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadFile()
    {
        $file = $this->request->file('file');
        if(!$file){
            return Response()->json(['code'=>201,'msg'=>'请上传文件']);
        }
        $folder_name = "/uploads/files/".date('Ym/d/',time());
        $upload_path = public_path() . "/" . $folder_name;
        $extension = strtolower($file->getClientOriginalExtension()) ?: 'png';
        $filename = time().rand(100,999).'.'. $extension;
        //将图片移动到我们目标存储路径中
        $file->move($upload_path , $filename);
        return Response()->json(['code'=>200,'msg'=>'上传成功','data'=>$folder_name.$filename]);

    }

}
